unit URelatorioSaldoNF;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, UrelatorioBase, Mask, ToolEdit, ExtCtrls, DBCtrls, StdCtrls,
  CheckLst, RLPDFFilter, RLXLSFilter, RLFilters, RLHTMLFilter,
  RLDraftFilter, DB, DBTables, RLReport, Buttons;

type
  TFRelatorioSaldoNF = class(TFrelatorioBase)
    DBLCBLocal: TDBLookupComboBox;
    Label3: TLabel;
    QLocal: TQuery;
    DSLocal: TDataSource;
    RLBand2: TRLBand;
    RLLabel1: TRLLabel;
    RLLabel2: TRLLabel;
    RLLabel3: TRLLabel;
    RLLabel4: TRLLabel;
    RLLabel5: TRLLabel;
    RLLabel6: TRLLabel;
    RLGroup1: TRLGroup;
    RLBand3: TRLBand;
    RLDBText2: TRLDBText;
    RLDBText3: TRLDBText;
    RLDBText4: TRLDBText;
    RLDBText5: TRLDBText;
    RLDBText6: TRLDBText;
    RLDBText7: TRLDBText;
    RLBand4: TRLBand;
    RLLabel7: TRLLabel;
    RLLabel8: TRLLabel;
    RLLabel9: TRLLabel;
    RLDBText8: TRLDBText;
    RadioGroup1: TRadioGroup;
    RLBand5: TRLBand;
    RLLabel10: TRLLabel;
    RLLabel11: TRLLabel;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnPesquisarClick(Sender: TObject);
    procedure RLBand3BeforePrint(Sender: TObject; var PrintIt: Boolean);
    procedure btnExecutarClick(Sender: TObject);
    procedure RLBand4AfterPrint(Sender: TObject);
    procedure DBLCBLocalKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure RLBand5BeforePrint(Sender: TObject; var PrintIt: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FRelatorioSaldoNF: TFRelatorioSaldoNF;
  QTD:integer;
  QTD_GERAL:integer;

implementation

uses UDmGestao, UGeral, PesquisaMovimentoEntradaSaidaEstoque,
  UPesquisaRapida;

{$R *.dfm}

procedure TFRelatorioSaldoNF.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  inherited;
  FRelatorioSaldoNF.Release ;
  FRelatorioSaldoNF := nil;
end;

procedure TFRelatorioSaldoNF.btnPesquisarClick(Sender: TObject);
var filtro:string;
begin
  filtro:='';

  filtro:=' where ( 1 = 1) ';

  if DBLCBLocal.KeyValue <> null then
   filtro:= filtro + ' AND (saldo_nf_item.saldonf_local = '+IntToStr(DBLCBLocal.KeyValue)+')';

  if RadioGroup1.ItemIndex = 1 then
    filtro:= filtro + ' AND (saldonf_qtd > 0)';

  QPesquisa.Close;
  QPesquisa.SQL.Clear;
  QPesquisa.SQL.Add('select * '+
                    ' from saldo_nf_item '+
                    ' inner join produto on (produto.prod_referencia = saldo_nf_item.saldonf_produto) '+
                      Filtro+' order by produto.prod_codigo');
  QPesquisa.Open;   
  TCurrencyField(QPesquisa.FieldByName('SALDONF_VALORUNITARIO')).DisplayFormat := '0,.0000';

  inherited;
end;

procedure TFRelatorioSaldoNF.RLBand3BeforePrint(Sender: TObject;
  var PrintIt: Boolean);
begin

  inherited;

  QTD:=QTD+DSPesquisa.DataSet.FieldByName('SALDONF_QTD').AsInteger;

  QTD_GERAL:= QTD_GERAL+ DSPesquisa.DataSet.FieldByName('SALDONF_QTD').AsInteger;

  RLLabel8.Caption:=  IntToStr(QTD);

end;

procedure TFRelatorioSaldoNF.btnExecutarClick(Sender: TObject);
begin
  QTD:=0;

  inherited;

end;

procedure TFRelatorioSaldoNF.RLBand4AfterPrint(Sender: TObject);
begin
  inherited;
qtd:=0;
end;

procedure TFRelatorioSaldoNF.DBLCBLocalKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  inherited;
  fun_dblookup(Key, Shift, DBLCBLocal);
end;

procedure TFRelatorioSaldoNF.RLBand5BeforePrint(Sender: TObject;
  var PrintIt: Boolean);
begin
  inherited;

 RLLabel11.Caption:= IntToStr(QTD_GERAL);

end;

end.
